package com.atguigu.study.interactive;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//    v1
//class AirCondition{
//    private int number=0;

//    public synchronized void increment() throws InterruptedException {
//     while(number!=0){
//         this.wait();
//     }
//        System.out.println(Thread.currentThread().getName()+"\t"+(++number));
//     this.notifyAll();
//    }
//    public synchronized void decrement() throws InterruptedException {
//        while(number==0){
//            this.wait();
//        }
//        System.out.println(Thread.currentThread().getName()+"\t"+(--number));
//        this.notifyAll();
//    }

//}

//v2
class AirCondition{
    private int number=0;
    final Lock lock=new ReentrantLock();
    final Condition condition=lock.newCondition();
    public void increment() throws InterruptedException {

        lock.lock();
        try {
            while (number!=0){
                condition.await();
            }
            System.out.println(Thread.currentThread().getName()+"\t"+(++number));
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
    public void decrement() throws InterruptedException {
           lock.lock();
        try {
            while (number==0){
                condition.await();
            }
            System.out.println(Thread.currentThread().getName()+"\t"+(--number));
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
}
public class ProdConsumerDemo {
    public static void main(String[] args) {
        AirCondition airCondition = new AirCondition();
        new Thread(()->{
                for (int i = 0; i < 10; i++) {
                    try{
                        TimeUnit.MILLISECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    try {
                        airCondition.increment();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
        },"A").start();

        new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    airCondition.decrement();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"B").start();

        new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(300);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    airCondition.increment();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"C").start();

        new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(400);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    airCondition.decrement();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"D").start();
    }
}
